package com.project.servlet;

import javax.servlet.http.HttpServletRequest;

import com.project.bean.Role;
import com.project.bo.RoleBO;
import com.project.enums.ServletResult;

import flexjson.JSONDeserializer;

/**
 * Servlet implementation class RoleServlet
 */
public class RoleServlet extends AbstractServlet {
	private static final long serialVersionUID = 1L;
       	
	@Override
	protected String validate(HttpServletRequest request) {
		return null;
	}

	@Override
	protected String insert(HttpServletRequest request) {
		String roleJSON = request.getParameter("role");
		String result = ServletResult.ERROR.toString();
		try {
		
			Role role = new JSONDeserializer<Role>().deserialize(roleJSON, Role.class);
			Boolean insertResult = Boolean.FALSE;
			if (role != null) {
				RoleBO roleBO = RoleBO.getInstance();
				insertResult = roleBO.insert(role);
				
				if (insertResult) {
					result = ServletResult.SUCCESS.toString();
				}
				
			}
			
		}catch (Exception e) {
			// TODO: Incluir Logs
		}
		
		return result;
	}

	@Override
	protected String disable(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		String result = ServletResult.ERROR.toString();
		try {
		
			if (roleId != null) {
				RoleBO roleBO = RoleBO.getInstance();
				
				if (roleBO.disable(Integer.parseInt(roleId))) {
					result = ServletResult.SUCCESS.toString();
				}

			}
			
		}catch (Exception e) {
			// TODO: Incluir Logs
		}
		
		return result;
	}

	@Override
	protected String update(HttpServletRequest request) {
		String roleJSON = request.getParameter("role");
		String result = ServletResult.ERROR.toString();
		try {
		
			Role role = new JSONDeserializer<Role>().deserialize(roleJSON, Role.class);
			Boolean updateResult = Boolean.FALSE;
			if (role != null) {
				RoleBO roleBO = RoleBO.getInstance();
				updateResult = roleBO.update(role);
				
				if (updateResult) {
					result = ServletResult.SUCCESS.toString();
				}
				
			}
			
		}catch (Exception e) {
			// TODO: Incluir Logs
		}
		
		return result;
	}

	@Override
	protected String enable(HttpServletRequest request) {
		String roleId = request.getParameter("roleId");
		String result = ServletResult.ERROR.toString();
		try {
		
			if (roleId != null) {
				RoleBO roleBO = RoleBO.getInstance();
				
				if (roleBO.enable(Integer.parseInt(roleId))) {
					result = ServletResult.SUCCESS.toString();
				}

			}
			
		}catch (Exception e) {
			// TODO: Incluir Logs
		}
		
		return result;
	}

	@Override
	protected String logout(HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

}
